package com.ycdx.impms.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.ycdx.impms.impl.UserImpl;
import com.ycdx.impms.pojo.User;
import com.ycdx.impms.utils.MD5Util;

public class UserAction extends ActionSupport implements ModelDriven<User>{

	private static final long serialVersionUID = -2593455889425684436L;
	
	private JSONObject result;//返回的json	
    private String rows;//每页显示的记录数        
    private String page;//当前第几页 
    
    private String uid;
    private String oldPass;
    private String newPass;
	
    private User user = new User();
    
    boolean success ;	
	String message;
    
	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String list(){
		//System.out.println(area);
		//当前页 
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page); 
        //每页显示条数 
        int number = Integer.parseInt((rows == null || rows == "0") ? "20":rows); 
        //每页的开始记录  第一页为1  第二页为number +1  
        int start = (intPage-1)*number; 
           
        List<User> list = UserImpl.getUserList(start, number);//每页的数据，放入list 
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map 
        jsonMap.put("total", UserImpl.getCount());//total键 存放总记录数，必须的 
        jsonMap.put("rows", list);//rows键 存放每页记录 list 
        result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
        //System.out.println(result);   
        //result = JSONArray.fromObject(jsonMap);   
        return SUCCESS; 

	}
	
	public String editPass(){
//		System.out.println(uid);
//		System.out.println(oldPass);
//		System.out.println(newPass);
		user = UserImpl.getUserById(uid);
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map 
		
		if(MD5Util.MD5(oldPass).equals(user.getPassword())){
			//密码一致，更新密码
			int flag = UserImpl.updateUserPass(uid,newPass);
			if(flag==1){
				//添加成功
				success = true;
				message = "密码修改成功，请牢记您的密码！";
			}else{
				//添加不成功
				success = false;
				message = "密码修改失败，请检查！";
			}
		}else{
			//密码不一致
			success = false;
			message = "您输入的原密码不正确，请重新输入！";
		}
		jsonMap.put("success", success);
        jsonMap.put("message", message);
        result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
        // System.out.println(result);
        return SUCCESS;
	}
	
	/**
	 * 添加人员信息
	 * @return
	 */
	public String add() {
		int flag = UserImpl.addUser(user);
		if(flag==1){
			//添加成功
			success = true;
			message = "用户信息添加成功,默认密码为123456！";
		}else{
			//添加不成功
			success = false;
			message = "用户信息添加失败，请检查！";
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map 
		jsonMap.put("success", success);
        jsonMap.put("message", message);
        result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
        // System.out.println(result);
        return SUCCESS;
	}
	/**
	 * 编辑用户信息
	 * @return
	 */
	public String edit(){
		//System.out.println(ryxx.getId());
		//System.out.println(ryxx.getDw());
		int flag = UserImpl.updateUser(user.getId(), user);
		if(flag==1){
			//添加成功
			success = true;
			message = "用户信息编辑成功！";
		}else{
			//添加不成功
			success = false;
			message = "用户信息编辑失败，请检查！";
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map 
		jsonMap.put("success", success);
        jsonMap.put("message", message);
        result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
        //System.out.println(result);
        return SUCCESS;
	}
	
	public String remove(){
		int flag = UserImpl.removeUser(user.getId());
		if(flag==1){
			//添加成功
			success = true;
			message = "编号为"+user.getId()+"的用户信息删除成功！";
		}else{
			//添加不成功
			success = false;
			message = "编号为"+user.getId()+"的用户信息删除失败，请检查！";
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map 
		jsonMap.put("success", success);
        jsonMap.put("message", message);
        result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
        //System.out.println(result);
        return SUCCESS;
	}
	
	public String reset(){
		int flag = UserImpl.resetUser(user.getId());
		if(flag==1){
			//添加成功
			success = true;
			message = "用户密码已重置成功,默认密码为123456！";
		}else{
			//添加不成功
			success = false;
			message = "用户密码重置失败，请检查！";
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map 
		jsonMap.put("success", success);
        jsonMap.put("message", message);
        result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject 
        //System.out.println(result);
        return SUCCESS;
	}
	
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}
	
	
	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getOldPass() {
		return oldPass;
	}

	public void setOldPass(String oldPass) {
		this.oldPass = oldPass;
	}

	public String getNewPass() {
		return newPass;
	}

	public void setNewPass(String newPass) {
		this.newPass = newPass;
	}
}
